package com.tian.factory;

import com.tian.enums.RepaymentCalculatorEnums;
import com.tian.service.LoanCalculatorPlainAverageCapitalStrategy;
import com.tian.service.LoanCalculatorPlainPrincipalInterestStrategy;
import com.tian.service.LoanCalculatorPlainScheduledDebtServicingStrategy;
import com.tian.service.LoanCalculatorPlainStrategy;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @author tianwc  公众号：java后端技术全栈、面试专栏
 * @version 1.0.0
 * @date 2023年09月20日 15:58
 * 在线刷题 1200+题和1000+篇干货文章：<a href="https://woaijava.cc/">博客地址</a>
 * <p>
 * 工厂模式+策略模式
 * <p>
 * 通过还款方式获取还款计划列表生成方式
 */
@Component
public class StrategyFactory {
    private final static Map<Integer, Class<?>> STRATEGY_MAP = new HashMap<>();
    @Resource
    private ApplicationContext applicationContext;

    public LoanCalculatorPlainStrategy getCalculatorStrategy(int type) {
        return (LoanCalculatorPlainStrategy) applicationContext.getBean(STRATEGY_MAP.get(type));
    }

    static {
        STRATEGY_MAP.put(RepaymentCalculatorEnums.AVERAGE_CAPITAL.getType(), LoanCalculatorPlainAverageCapitalStrategy.class);
        STRATEGY_MAP.put(RepaymentCalculatorEnums.PRINCIPAL_INTEREST.getType(), LoanCalculatorPlainPrincipalInterestStrategy.class);
        STRATEGY_MAP.put(RepaymentCalculatorEnums.SCHEDULED_DEBT_SERVICING.getType(), LoanCalculatorPlainScheduledDebtServicingStrategy.class);
    }
}
